/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Util;

import javax.crypto.Cipher;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.InvalidKeyException;

/**
 *
 * @author Abhishek Agrawal
 */
public class UserAccountUtilNA {

    private static String algorithm = "DESede";
    private static Key key = null;
    private static Cipher cipher = null;
    private static UserAccountUtilNA obj = new UserAccountUtilNA();

    public UserAccountUtilNA() {
        try {
            key = KeyGenerator.getInstance(algorithm).generateKey();
            cipher = Cipher.getInstance(algorithm);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static UserAccountUtilNA getInstance() {
        return obj;
    }

    public static byte[] encrypt(String inputPassword) throws InvalidKeyException,
            BadPaddingException,
            IllegalBlockSizeException {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] inputBytes = inputPassword.getBytes();
        return cipher.doFinal(inputBytes);
    }

    public static String getEncryptStringValue(String inputPassword) throws InvalidKeyException,
            BadPaddingException,
            IllegalBlockSizeException {
        return new String(encrypt(inputPassword));
    }

    public static String decrypt(byte[] encryptionBytes) throws InvalidKeyException,
            BadPaddingException,
            IllegalBlockSizeException {
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
        String recoveredPassword = new String(recoveredBytes);
        return recoveredPassword;
    }

    public static String getDecryptStringValue(byte[] encryptionBytes) throws InvalidKeyException,
            BadPaddingException,
            IllegalBlockSizeException {
        return decrypt(encryptionBytes);
    }

   
//    public String encryptPassword(String loginPassword) {
//        byte[] digestedPassword = null;
//        try {
//            MessageDigest md = MessageDigest.getInstance("SHA-1");
//            String password = loginPassword;
//            md.update(password.getBytes());
//            digestedPassword = md.digest();
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//            return null;
//        } finally {
//            return new String(digestedPassword);
//        }
//    }
//
//    public String decryptPassword(String userPassword) {
//        MessageDigest sha = null;
//        try {
//            sha = MessageDigest.getInstance("MD5");
//            byte[] password = userPassword.getBytes();
//            sha.update(password);
//        } catch (java.security.NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        } finally {
//            return new String(sha.digest());
//        }
//    }
}
